######################################################
#Partisanship Interaction- 2018 General Election PA.
# When Women Run, Voters Will Follow (Sometimes): 
##Examining the Mobilizing Effect of Female Candidates in the 2014 and 2018 Midterm Elections 
#By Safarpour, Wyckoff Gaynor, Rouse, and Swers #
######################################################

#clean the environment.
rm(list=ls()) 

#Packages.
library(mvtnorm) 
library(stargazer)
library(stats)
library(arm)
library(plyr)
library(Hmisc)
library(dplyr)
library(lme4)
library(margins)

#setwd
setwd("/Users/ACS/Dropbox/When Women Run/Revision_PoliticalBehavior/R&R Part 2/Publication Docs/Replication Data and Code/")

#Step 1. Bring in fully recoded dataset.

PA<- read.csv("PA2018GeneralDataFinal.csv", header=TRUE)

#Step 2. Check length and column names.
length(PA$V1)
names(PA)
#Step 3. Run Model.

m1<-glm(votedGeneral~ FemaleDemocrat+FemaleRepublican+
          female2+FemaleDemocrat*female2+FemaleRepublican*female2+
          GenerationZ+FemaleDemocrat*GenerationZ+FemaleRepublican*GenerationZ+
          Millennials+FemaleDemocrat*Millennials+FemaleRepublican*Millennials+
          GenerationX+FemaleDemocrat*GenerationX+FemaleRepublican*GenerationX+
          SilentGeneration+FemaleDemocrat*SilentGeneration+FemaleRepublican*SilentGeneration+
          rep + FemaleDemocrat*rep+FemaleRepublican*rep+
          otherparty+ FemaleDemocrat*otherparty+FemaleRepublican*otherparty+
          female2*rep+ female2*otherparty+
          voted2016r+Proptrumpvote2016+
          otherparty*FemaleDemocrat*female2+
          otherparty*FemaleRepublican*female2+
          rep*FemaleDemocrat*female2+
          rep*FemaleRepublican*female2+totalreceipts, 
        data=PA, family=binomial(link="logit"))
summary(m1)

summary(margins(m1, variables=c("FemaleDemocrat"))) 

summary(margins(m1, variables=c("FemaleDemocrat"), at=list(female2=0:1, rep=0, otherparty=0))) 



stargazer(m1, out="PA2018GeneralPartyInteractionRevised.html",
          type="html",
          covariate.labels = c("Female Democratic Candidate", 
                               "Female Republican Candidate",
                               "Female", "Generation Z",
                               "Millennials", "Generation X","Silent Generation", 
                               "Republican", "Other Party",
                               "Voted in 2016 General Election", "County Trump Vote 2016", 
                               "Total Money Raised",
                               "Female Democratic Candidate*Female",
                               "Female Republican Candidate*Female",
                               "Female Democratic Candidate*Generation Z",
                               "Female Republican Candidate*Generation Z",
                               "Female Democratic Candidate*Millennials",
                               "Female Republican Candidate*Millennials",
                               "Female Democratic Candidate*Generation X",                               
                               "Female Republican Candidate*Generation X",
                               "Female Democratic Candidate*Silent Generation",
                               "Female Republican Candidate*Silent Generation",
                               "Female Democratic Candidate*Republican",
                               "Female Republican Candidate*Republican",
                               "Female Democratic Candidate*Other Party",
                               "Female Republican Candidate*Other Party",
                               "Female*Republican", "Female*Other Party",
                               "Other Party*Female Democratic Candidate*Female",
                               "Other Party*Female Republican Candidate*Female",
                               "Republican*Female Democratic Candidate*Female",
                               "Republican*Female Republican Candidate*Female"),
          dep.var.labels="Voted in PA 2018 General Election", column.labels = "",
          title = "Effects of candidate gender, generation, gender,  party, prior voting, county Trump vote share, and total money raised on 2018 Midterm Turnout in PA",
          notes        = "Results from logistic regression. Standard errors in parentheses. Baseline age category: Baby Boomers. Baseline party: Democrats. Baseline district: only male House candidates. Baseline gender: Male. The only district with a female Republican candidate also had a female Democratic candidate (district 5).", 
          notes.append = TRUE, notes.align = "l", digits=3, single.row = T)

sample <- PA[complete.cases(PA$votedGeneral, PA$FemaleDemocrat, PA$FemaleRepublican, PA$SilentGeneration, 
                            PA$Millennials, PA$GenerationZ, PA$GenerationX, PA$female2,
                            PA$rep, PA$otherparty, PA$voted2016r, PA$Proptrumpvote2016, PA$totalreceipts)==T,] # Regression sample
str(sample)
##Overall effect for m1.
preds <- predict(m1, type = "response")
options(digits=4)
summary(preds) #mean turnout.

n_draws <- 1000
set.seed(1714)
vcovm1<- vcov(m1)
coefm1<-coef(m1)
sim_coefs_m1 <- rmvnorm(n_draws, coefm1, vcovm1) #Specify the 1,000 simulated coefficients.
rbind(coef(m1), apply(sim_coefs_m1, 2, mean)) # Check they are close to original


ppwomen0m1s <- NULL
for (i in 1:n_draws) {
  ppwomen0m1s[i] <- mean(invlogit(sim_coefs_m1[i,1] + sim_coefs_m1[i,2]*0 + #Female Democrat
                                    sim_coefs_m1[i,3]*0 + #FemaleRepublican
                                    sim_coefs_m1[i,4]*sample$female2 + 
                                    sim_coefs_m1[i,5]*sample$GenerationZ +
                                    sim_coefs_m1[i,6]*sample$Millennials +
                                    sim_coefs_m1[i,7]*sample$GenerationX +
                                    sim_coefs_m1[i,8]*sample$SilentGeneration+
                                    sim_coefs_m1[i,9]*sample$rep +
                                    sim_coefs_m1[i,10]*sample$otherparty+
                                    sim_coefs_m1[i,11]*sample$voted2016r+
                                    sim_coefs_m1[i,12]*sample$Proptrumpvote2016+
                                    sim_coefs_m1[i,13]*sample$totalreceipts+
                                    sim_coefs_m1[i,14]*sample$female2*0+ #Female Democrat*Female
                                    sim_coefs_m1[i,15]*sample$female2*0+ #FemaleRepublican:female2
                                    sim_coefs_m1[i,16]*sample$GenerationZ*0 + #FemaleDemocrat:GenerationZ
                                    sim_coefs_m1[i,17]*sample$GenerationZ*0 + #FemaleRepublican:GenerationZ
                                    sim_coefs_m1[i,18]*sample$Millennials*0 + #FemaleDemocrat:Millennials
                                    sim_coefs_m1[i,19]*sample$Millennials*0 + #FemaleRepublican:Millennials
                                    sim_coefs_m1[i,20]*sample$GenerationX*0 + #FemaleDemocrat:GenerationX
                                    sim_coefs_m1[i,21]*sample$GenerationX*0 + #FemaleRepublican:GenerationX
                                    sim_coefs_m1[i,22]*sample$SilentGeneration*0+ #FemaleDemocrat:SilentGeneration
                                    sim_coefs_m1[i,23]*sample$SilentGeneration*0+ #FemaleRepublican:SilentGeneration
                                    sim_coefs_m1[i,24]*sample$rep*0 + #FemaleDemocrat:rep
                                    sim_coefs_m1[i,25]*sample$rep*0 + #FemaleRepublican:rep
                                    sim_coefs_m1[i,26]*sample$otherparty*0+ #FemaleDemocrat:otherparty
                                    sim_coefs_m1[i,27]*sample$otherparty*0+ #FemaleRepublican:otherparty
                                    sim_coefs_m1[i,28]*sample$female2*sample$rep+ #female2:rep
                                    sim_coefs_m1[i,29]*sample$female2*sample$otherparty+ #female2:otherparty
                                    sim_coefs_m1[i,30]*sample$female2*sample$otherparty*0+ #FemaleDemocrat:female2:otherparty
                                    sim_coefs_m1[i,31]*sample$female2*sample$otherparty*0+ #FemaleRepublican:female2:otherparty
                                    sim_coefs_m1[i,32]*sample$female2*sample$rep*0+ #FemaleDemocrat:female2:rep
                                    sim_coefs_m1[i,33]*sample$female2*sample$rep*0 #FemaleRepublican:female2:rep
  ))
}
mean(ppwomen0m1s) # 

ppwomen1m1.s <- NULL
for (i in 1:n_draws) {
  ppwomen1m1.s[i] <- mean(invlogit(sim_coefs_m1[i,1] + sim_coefs_m1[i,2]*1 + #Female Democrat
                                     sim_coefs_m1[i,3]*1 + #FemaleRepublican
                                     sim_coefs_m1[i,4]*sample$female2 + 
                                     sim_coefs_m1[i,5]*sample$GenerationZ +
                                     sim_coefs_m1[i,6]*sample$Millennials +
                                     sim_coefs_m1[i,7]*sample$GenerationX +
                                     sim_coefs_m1[i,8]*sample$SilentGeneration+
                                     sim_coefs_m1[i,9]*sample$rep +
                                     sim_coefs_m1[i,10]*sample$otherparty+
                                     sim_coefs_m1[i,11]*sample$voted2016r+
                                     sim_coefs_m1[i,12]*sample$Proptrumpvote2016+
                                     sim_coefs_m1[i,13]*sample$totalreceipts+
                                     sim_coefs_m1[i,14]*sample$female2*1+ #Female Democrat*Female
                                     sim_coefs_m1[i,15]*sample$female2*1+ #FemaleRepublican:female2
                                     sim_coefs_m1[i,16]*sample$GenerationZ*1 + #FemaleDemocrat:GenerationZ
                                     sim_coefs_m1[i,17]*sample$GenerationZ*1 + #FemaleRepublican:GenerationZ
                                     sim_coefs_m1[i,18]*sample$Millennials*1 + #FemaleDemocrat:Millennials
                                     sim_coefs_m1[i,19]*sample$Millennials*1 + #FemaleRepublican:Millennials
                                     sim_coefs_m1[i,20]*sample$GenerationX*1 + #FemaleDemocrat:GenerationX
                                     sim_coefs_m1[i,21]*sample$GenerationX*1 + #FemaleRepublican:GenerationX
                                     sim_coefs_m1[i,22]*sample$SilentGeneration*1+ #FemaleDemocrat:SilentGeneration
                                     sim_coefs_m1[i,23]*sample$SilentGeneration*1+ #FemaleRepublican:SilentGeneration
                                     sim_coefs_m1[i,24]*sample$rep*1 + #FemaleDemocrat:rep
                                     sim_coefs_m1[i,25]*sample$rep*1 + #FemaleRepublican:rep
                                     sim_coefs_m1[i,26]*sample$otherparty*1+ #FemaleDemocrat:otherparty
                                     sim_coefs_m1[i,27]*sample$otherparty*1+ #FemaleRepublican:otherparty
                                     sim_coefs_m1[i,28]*sample$female2*sample$rep+ #female2:rep
                                     sim_coefs_m1[i,29]*sample$female2*sample$otherparty+ #female2:otherparty
                                     sim_coefs_m1[i,30]*sample$female2*sample$otherparty*1+ #FemaleDemocrat:female2:otherparty
                                     sim_coefs_m1[i,31]*sample$female2*sample$otherparty*1+ #FemaleRepublican:female2:otherparty
                                     sim_coefs_m1[i,32]*sample$female2*sample$rep*1+ #FemaleDemocrat:female2:rep
                                     sim_coefs_m1[i,33]*sample$female2*sample$rep*1 #FemaleRepublican:female2:rep
  ))
}
mean(ppwomen1m1.s) #


effect.01m1 <-  ppwomen1m1.s-ppwomen0m1s
summary(effect.01m1) #  (0-1 women candidate effect)

# CIs and put results together in matrix
elementsm1 <- list(ppwomen0m1s, ppwomen1m1.s, effect.01m1) # I split these two lines so the code is clear.
lapply(elementsm1, summary) # See summary for each element of the list
cim1 <- do.call("rbind", (lapply(elementsm1, quantile, c(.025,.975)))) # Get quntiles for each

resultsm1 <- cbind(cim1[,1], lapply(elementsm1, mean), cim1[,2])
colnames(resultsm1) <- c("2.5", "Mean", "97.5")   
rownames(resultsm1) <- c("No Women Ran", "Female Candidate", "Effect No Women-Female Candidate")
resultsm1 #

#Drop what we no longer need.
rm(ppwomen0m1s, ppwomen1m1.s, effect.01m1, cim1)

# Store mean effect and 95% CI to visualize results of m1 later.
meaneffect_m1<-resultsm1[[6]]
upperci_m1<-resultsm1[[3]]
lowerci_m1<-resultsm1[[9]]

#Only effect of Female Democrats-------
w0.FDEM<-NULL
w1.FDEM<-NULL

w0.FDEM <- NULL
for (i in 1:n_draws) {
  w0.FDEM[i] <- mean(invlogit(sim_coefs_m1[i,1] + sim_coefs_m1[i,2]*0 + #Female Democrat
                                sim_coefs_m1[i,3]*sample$FemaleRepublican + #FemaleRepublican (obs val)
                                sim_coefs_m1[i,4]*sample$female2 + 
                                sim_coefs_m1[i,5]*sample$GenerationZ +
                                sim_coefs_m1[i,6]*sample$Millennials +
                                sim_coefs_m1[i,7]*sample$GenerationX +
                                sim_coefs_m1[i,8]*sample$SilentGeneration+
                                sim_coefs_m1[i,9]*sample$rep +
                                sim_coefs_m1[i,10]*sample$otherparty+
                                sim_coefs_m1[i,11]*sample$voted2016r+
                                sim_coefs_m1[i,12]*sample$Proptrumpvote2016+
                                sim_coefs_m1[i,13]*sample$totalreceipts+
                                sim_coefs_m1[i,14]*sample$female2*0+ #Female Democrat*Female
                                sim_coefs_m1[i,15]*sample$female2*sample$FemaleRepublican+ #FemaleRepublican:female2
                                sim_coefs_m1[i,16]*sample$GenerationZ*0 + #FemaleDemocrat:GenerationZ
                                sim_coefs_m1[i,17]*sample$GenerationZ*sample$FemaleRepublican + #FemaleRepublican:GenerationZ
                                sim_coefs_m1[i,18]*sample$Millennials*0 + #FemaleDemocrat:Millennials
                                sim_coefs_m1[i,19]*sample$Millennials*sample$FemaleRepublican + #FemaleRepublican:Millennials
                                sim_coefs_m1[i,20]*sample$GenerationX*0 + #FemaleDemocrat:GenerationX
                                sim_coefs_m1[i,21]*sample$GenerationX*sample$FemaleRepublican + #FemaleRepublican:GenerationX
                                sim_coefs_m1[i,22]*sample$SilentGeneration*0+ #FemaleDemocrat:SilentGeneration
                                sim_coefs_m1[i,23]*sample$SilentGeneration*sample$FemaleRepublican+ #FemaleRepublican:SilentGeneration
                                sim_coefs_m1[i,24]*sample$rep*0 + #FemaleDemocrat:rep
                                sim_coefs_m1[i,25]*sample$rep*sample$FemaleRepublican + #FemaleRepublican:rep
                                sim_coefs_m1[i,26]*sample$otherparty*0+ #FemaleDemocrat:otherparty
                                sim_coefs_m1[i,27]*sample$otherparty*sample$FemaleRepublican+ #FemaleRepublican:otherparty
                                sim_coefs_m1[i,28]*sample$female2*sample$rep+ #female2:rep
                                sim_coefs_m1[i,29]*sample$female2*sample$otherparty+ #female2:otherparty
                                sim_coefs_m1[i,30]*sample$female2*sample$otherparty*0+ #FemaleDemocrat:female2:otherparty
                                sim_coefs_m1[i,31]*sample$female2*sample$otherparty*sample$FemaleRepublican+ #FemaleRepublican:female2:otherparty
                                sim_coefs_m1[i,32]*sample$female2*sample$rep*0+ #FemaleDemocrat:female2:rep
                                sim_coefs_m1[i,33]*sample$female2*sample$rep*sample$FemaleRepublican #FemaleRepublican:female2:rep
  ))
}
mean(w0.FDEM) # 

w1.FDEM <- NULL
for (i in 1:n_draws) {
  w1.FDEM[i] <- mean(invlogit(sim_coefs_m1[i,1] + sim_coefs_m1[i,2]*1 + #Female Democrat
                                sim_coefs_m1[i,3]*sample$FemaleRepublican + #FemaleRepublican (obs val)
                                sim_coefs_m1[i,4]*sample$female2 + 
                                sim_coefs_m1[i,5]*sample$GenerationZ +
                                sim_coefs_m1[i,6]*sample$Millennials +
                                sim_coefs_m1[i,7]*sample$GenerationX +
                                sim_coefs_m1[i,8]*sample$SilentGeneration+
                                sim_coefs_m1[i,9]*sample$rep +
                                sim_coefs_m1[i,10]*sample$otherparty+
                                sim_coefs_m1[i,11]*sample$voted2016r+
                                sim_coefs_m1[i,12]*sample$Proptrumpvote2016+                                    
                                sim_coefs_m1[i,13]*sample$totalreceipts+
                                sim_coefs_m1[i,14]*sample$female2*1+ #Female Democrat*Female
                                sim_coefs_m1[i,15]*sample$female2*sample$FemaleRepublican+ #FemaleRepublican:female2
                                sim_coefs_m1[i,16]*sample$GenerationZ*1 + #FemaleDemocrat:GenerationZ
                                sim_coefs_m1[i,17]*sample$GenerationZ*sample$FemaleRepublican + #FemaleRepublican:GenerationZ
                                sim_coefs_m1[i,18]*sample$Millennials*1 + #FemaleDemocrat:Millennials
                                sim_coefs_m1[i,19]*sample$Millennials*sample$FemaleRepublican + #FemaleRepublican:Millennials
                                sim_coefs_m1[i,20]*sample$GenerationX*1 + #FemaleDemocrat:GenerationX
                                sim_coefs_m1[i,21]*sample$GenerationX*sample$FemaleRepublican + #FemaleRepublican:GenerationX
                                sim_coefs_m1[i,22]*sample$SilentGeneration*1+ #FemaleDemocrat:SilentGeneration
                                sim_coefs_m1[i,23]*sample$SilentGeneration*sample$FemaleRepublican+ #FemaleRepublican:SilentGeneration
                                sim_coefs_m1[i,24]*sample$rep*1 + #FemaleDemocrat:rep
                                sim_coefs_m1[i,25]*sample$rep*sample$FemaleRepublican + #FemaleRepublican:rep
                                sim_coefs_m1[i,26]*sample$otherparty*1+ #FemaleDemocrat:otherparty
                                sim_coefs_m1[i,27]*sample$otherparty*sample$FemaleRepublican+ #FemaleRepublican:otherparty
                                sim_coefs_m1[i,28]*sample$female2*sample$rep+ #female2:rep
                                sim_coefs_m1[i,29]*sample$female2*sample$otherparty+ #female2:otherparty
                                sim_coefs_m1[i,30]*sample$female2*sample$otherparty*1+ #FemaleDemocrat:female2:otherparty
                                sim_coefs_m1[i,31]*sample$female2*sample$otherparty*sample$FemaleRepublican+ #FemaleRepublican:female2:otherparty
                                sim_coefs_m1[i,32]*sample$female2*sample$rep*1+ #FemaleDemocrat:female2:rep
                                sim_coefs_m1[i,33]*sample$female2*sample$rep*sample$FemaleRepublican #FemaleRepublican:female2:rep
  ))
}
mean(w1.FDEM) #


effect.FDEM	<- w1.FDEM - w0.FDEM 
quantile(effect.FDEM, c(.025,.975)) #to get CIs; 
mean(effect.FDEM) #to get mean effect

#Drop what we no longer need.
rm(w1.FDEM, w0.FDEM )

#Only Effect of Female Republicans------
w0.FGOP<-NULL
w1.FGOP<-NULL

w0.FGOP <- NULL
for (i in 1:n_draws) {
  w0.FGOP[i] <- mean(invlogit(sim_coefs_m1[i,1] + sim_coefs_m1[i,2]*sample$FemaleDemocrat + #Female Democrat (obs val)
                                sim_coefs_m1[i,3]*0 + #FemaleRepublican 
                                sim_coefs_m1[i,4]*sample$female2 + 
                                sim_coefs_m1[i,5]*sample$GenerationZ +
                                sim_coefs_m1[i,6]*sample$Millennials +
                                sim_coefs_m1[i,7]*sample$GenerationX +
                                sim_coefs_m1[i,8]*sample$SilentGeneration+
                                sim_coefs_m1[i,9]*sample$rep +
                                sim_coefs_m1[i,10]*sample$otherparty+
                                sim_coefs_m1[i,11]*sample$voted2016r+
                                sim_coefs_m1[i,12]*sample$Proptrumpvote2016+
                                sim_coefs_m1[i,13]*sample$totalreceipts+
                                sim_coefs_m1[i,14]*sample$female2*sample$FemaleDemocrat+ #Female Democrat*Female
                                sim_coefs_m1[i,15]*sample$female2*0+ #FemaleRepublican:female2
                                sim_coefs_m1[i,16]*sample$GenerationZ*sample$FemaleDemocrat + #FemaleDemocrat:GenerationZ
                                sim_coefs_m1[i,17]*sample$GenerationZ*0 + #FemaleRepublican:GenerationZ
                                sim_coefs_m1[i,18]*sample$Millennials*sample$FemaleDemocrat + #FemaleDemocrat:Millennials
                                sim_coefs_m1[i,19]*sample$Millennials*0 + #FemaleRepublican:Millennials
                                sim_coefs_m1[i,20]*sample$GenerationX*sample$FemaleDemocrat + #FemaleDemocrat:GenerationX
                                sim_coefs_m1[i,21]*sample$GenerationX*0 + #FemaleRepublican:GenerationX
                                sim_coefs_m1[i,22]*sample$SilentGeneration*sample$FemaleDemocrat+ #FemaleDemocrat:SilentGeneration
                                sim_coefs_m1[i,23]*sample$SilentGeneration*0+ #FemaleRepublican:SilentGeneration
                                sim_coefs_m1[i,24]*sample$rep*sample$FemaleDemocrat + #FemaleDemocrat:rep
                                sim_coefs_m1[i,25]*sample$rep*0 + #FemaleRepublican:rep
                                sim_coefs_m1[i,26]*sample$otherparty*sample$FemaleDemocrat+ #FemaleDemocrat:otherparty
                                sim_coefs_m1[i,27]*sample$otherparty*0+ #FemaleRepublican:otherparty
                                sim_coefs_m1[i,28]*sample$female2*sample$rep+ #female2:rep
                                sim_coefs_m1[i,29]*sample$female2*sample$otherparty+ #female2:otherparty
                                sim_coefs_m1[i,30]*sample$female2*sample$otherparty*sample$FemaleDemocrat+ #FemaleDemocrat:female2:otherparty
                                sim_coefs_m1[i,31]*sample$female2*sample$otherparty*0+ #FemaleRepublican:female2:otherparty
                                sim_coefs_m1[i,32]*sample$female2*sample$rep*sample$FemaleDemocrat+ #FemaleDemocrat:female2:rep
                                sim_coefs_m1[i,33]*sample$female2*sample$rep*0 #FemaleRepublican:female2:rep
  ))
}
mean(w0.FGOP) # 


for (i in 1:n_draws) {
  w1.FGOP[i] <- mean(invlogit(sim_coefs_m1[i,1] + sim_coefs_m1[i,2]*sample$FemaleDemocrat + #Female Democrat (obs val)
                                sim_coefs_m1[i,3]*1 + #FemaleRepublican 
                                sim_coefs_m1[i,4]*sample$female2 + 
                                sim_coefs_m1[i,5]*sample$GenerationZ +
                                sim_coefs_m1[i,6]*sample$Millennials +
                                sim_coefs_m1[i,7]*sample$GenerationX +
                                sim_coefs_m1[i,8]*sample$SilentGeneration+
                                sim_coefs_m1[i,9]*sample$rep +
                                sim_coefs_m1[i,10]*sample$otherparty+
                                sim_coefs_m1[i,11]*sample$voted2016r+
                                sim_coefs_m1[i,12]*sample$Proptrumpvote2016+
                                sim_coefs_m1[i,13]*sample$totalreceipts+
                                sim_coefs_m1[i,14]*sample$female2*sample$FemaleDemocrat+ #Female Democrat*Female
                                sim_coefs_m1[i,15]*sample$female2*1+ #FemaleRepublican:female2
                                sim_coefs_m1[i,16]*sample$GenerationZ*sample$FemaleDemocrat + #FemaleDemocrat:GenerationZ
                                sim_coefs_m1[i,17]*sample$GenerationZ*1 + #FemaleRepublican:GenerationZ
                                sim_coefs_m1[i,18]*sample$Millennials*sample$FemaleDemocrat + #FemaleDemocrat:Millennials
                                sim_coefs_m1[i,19]*sample$Millennials*1 + #FemaleRepublican:Millennials
                                sim_coefs_m1[i,20]*sample$GenerationX*sample$FemaleDemocrat + #FemaleDemocrat:GenerationX
                                sim_coefs_m1[i,21]*sample$GenerationX*1 + #FemaleRepublican:GenerationX
                                sim_coefs_m1[i,22]*sample$SilentGeneration*sample$FemaleDemocrat+ #FemaleDemocrat:SilentGeneration
                                sim_coefs_m1[i,23]*sample$SilentGeneration*1+ #FemaleRepublican:SilentGeneration
                                sim_coefs_m1[i,24]*sample$rep*sample$FemaleDemocrat + #FemaleDemocrat:rep
                                sim_coefs_m1[i,25]*sample$rep*1 + #FemaleRepublican:rep
                                sim_coefs_m1[i,26]*sample$otherparty*sample$FemaleDemocrat+ #FemaleDemocrat:otherparty
                                sim_coefs_m1[i,27]*sample$otherparty*1+ #FemaleRepublican:otherparty
                                sim_coefs_m1[i,28]*sample$female2*sample$rep+ #female2:rep
                                sim_coefs_m1[i,29]*sample$female2*sample$otherparty+ #female2:otherparty
                                sim_coefs_m1[i,30]*sample$female2*sample$otherparty*sample$FemaleDemocrat+ #FemaleDemocrat:female2:otherparty
                                sim_coefs_m1[i,31]*sample$female2*sample$otherparty*1+ #FemaleRepublican:female2:otherparty
                                sim_coefs_m1[i,32]*sample$female2*sample$rep*sample$FemaleDemocrat+ #FemaleDemocrat:female2:rep
                                sim_coefs_m1[i,33]*sample$female2*sample$rep*1 #FemaleRepublican:female2:rep
  ))
}
mean(w1.FGOP) #
effect.FGOP	<- w1.FGOP - w0.FGOP 
quantile(effect.FGOP, c(.025,.975)) #to get CIs; 
mean(effect.FGOP) #to get mean effect

#Drop what we no longer need.
rm(w0.FGOP, w1.FGOP )

#Effect of Female Democrats on Democrats overall----
DEM <- subset(sample, dem==1) 

w0.DEM<-NULL
w1.DEM<- NULL

for (i in 1:n_draws) {
  w0.DEM[i] <- mean(invlogit(sim_coefs_m1[i,1] + sim_coefs_m1[i,2]*0 + #Female Democrat
                               sim_coefs_m1[i,3]*DEM$FemaleRepublican + #FemaleRepublican (obs val)
                               sim_coefs_m1[i,4]*DEM$female2 + 
                               sim_coefs_m1[i,5]*DEM$GenerationZ +
                               sim_coefs_m1[i,6]*DEM$Millennials +
                               sim_coefs_m1[i,7]*DEM$GenerationX +
                               sim_coefs_m1[i,8]*DEM$SilentGeneration+
                               sim_coefs_m1[i,9]*DEM$rep +
                               sim_coefs_m1[i,10]*DEM$otherparty+
                               sim_coefs_m1[i,11]*DEM$voted2016r+
                               sim_coefs_m1[i,12]*DEM$Proptrumpvote2016+
                               sim_coefs_m1[i,13]*DEM$totalreceipts+
                               sim_coefs_m1[i,14]*DEM$female2*0+ #Female Democrat*Female
                               sim_coefs_m1[i,15]*DEM$female2*DEM$FemaleRepublican+ #FemaleRepublican:female2
                               sim_coefs_m1[i,16]*DEM$GenerationZ*0 + #FemaleDemocrat:GenerationZ
                               sim_coefs_m1[i,17]*DEM$GenerationZ*DEM$FemaleRepublican + #FemaleRepublican:GenerationZ
                               sim_coefs_m1[i,18]*DEM$Millennials*0 + #FemaleDemocrat:Millennials
                               sim_coefs_m1[i,19]*DEM$Millennials*DEM$FemaleRepublican + #FemaleRepublican:Millennials
                               sim_coefs_m1[i,20]*DEM$GenerationX*0 + #FemaleDemocrat:GenerationX
                               sim_coefs_m1[i,21]*DEM$GenerationX*DEM$FemaleRepublican + #FemaleRepublican:GenerationX
                               sim_coefs_m1[i,22]*DEM$SilentGeneration*0+ #FemaleDemocrat:SilentGeneration
                               sim_coefs_m1[i,23]*DEM$SilentGeneration*DEM$FemaleRepublican+ #FemaleRepublican:SilentGeneration
                               sim_coefs_m1[i,24]*DEM$rep*0 + #FemaleDemocrat:rep
                               sim_coefs_m1[i,25]*DEM$rep*DEM$FemaleRepublican + #FemaleRepublican:rep
                               sim_coefs_m1[i,26]*DEM$otherparty*0+ #FemaleDemocrat:otherparty
                               sim_coefs_m1[i,27]*DEM$otherparty*DEM$FemaleRepublican+ #FemaleRepublican:otherparty
                               sim_coefs_m1[i,28]*DEM$female2*DEM$rep+ #female2:rep
                               sim_coefs_m1[i,29]*DEM$female2*DEM$otherparty+ #female2:otherparty
                               sim_coefs_m1[i,30]*DEM$female2*DEM$otherparty*0+ #FemaleDemocrat:female2:otherparty
                               sim_coefs_m1[i,31]*DEM$female2*DEM$otherparty*DEM$FemaleRepublican+ #FemaleRepublican:female2:otherparty
                               sim_coefs_m1[i,32]*DEM$female2*DEM$rep*0+ #FemaleDemocrat:female2:rep
                               sim_coefs_m1[i,33]*DEM$female2*DEM$rep*DEM$FemaleRepublican #FemaleRepublican:female2:rep
  ))
}
mean(w0.DEM) # 


for (i in 1:n_draws) {
  w1.DEM[i] <- mean(invlogit(sim_coefs_m1[i,1] + sim_coefs_m1[i,2]*1 + #Female Democrat
                               sim_coefs_m1[i,3]*DEM$FemaleRepublican + #FemaleRepublican (obs val)
                               sim_coefs_m1[i,4]*DEM$female2 + 
                               sim_coefs_m1[i,5]*DEM$GenerationZ +
                               sim_coefs_m1[i,6]*DEM$Millennials +
                               sim_coefs_m1[i,7]*DEM$GenerationX +
                               sim_coefs_m1[i,8]*DEM$SilentGeneration+
                               sim_coefs_m1[i,9]*DEM$rep +
                               sim_coefs_m1[i,10]*DEM$otherparty+
                               sim_coefs_m1[i,11]*DEM$voted2016r+
                               sim_coefs_m1[i,12]*DEM$Proptrumpvote2016+
                               sim_coefs_m1[i,13]*DEM$totalreceipts+
                               sim_coefs_m1[i,14]*DEM$female2*1+ #Female Democrat*Female
                               sim_coefs_m1[i,15]*DEM$female2*DEM$FemaleRepublican+ #FemaleRepublican:female2
                               sim_coefs_m1[i,16]*DEM$GenerationZ*1 + #FemaleDemocrat:GenerationZ
                               sim_coefs_m1[i,17]*DEM$GenerationZ*DEM$FemaleRepublican + #FemaleRepublican:GenerationZ
                               sim_coefs_m1[i,18]*DEM$Millennials*1 + #FemaleDemocrat:Millennials
                               sim_coefs_m1[i,19]*DEM$Millennials*DEM$FemaleRepublican + #FemaleRepublican:Millennials
                               sim_coefs_m1[i,20]*DEM$GenerationX*1 + #FemaleDemocrat:GenerationX
                               sim_coefs_m1[i,21]*DEM$GenerationX*DEM$FemaleRepublican + #FemaleRepublican:GenerationX
                               sim_coefs_m1[i,22]*DEM$SilentGeneration*1+ #FemaleDemocrat:SilentGeneration
                               sim_coefs_m1[i,23]*DEM$SilentGeneration*DEM$FemaleRepublican+ #FemaleRepublican:SilentGeneration
                               sim_coefs_m1[i,24]*DEM$rep*1 + #FemaleDemocrat:rep
                               sim_coefs_m1[i,25]*DEM$rep*DEM$FemaleRepublican + #FemaleRepublican:rep
                               sim_coefs_m1[i,26]*DEM$otherparty*1+ #FemaleDemocrat:otherparty
                               sim_coefs_m1[i,27]*DEM$otherparty*DEM$FemaleRepublican+ #FemaleRepublican:otherparty
                               sim_coefs_m1[i,28]*DEM$female2*DEM$rep+ #female2:rep
                               sim_coefs_m1[i,29]*DEM$female2*DEM$otherparty+ #female2:otherparty
                               sim_coefs_m1[i,30]*DEM$female2*DEM$otherparty*1+ #FemaleDemocrat:female2:otherparty
                               sim_coefs_m1[i,31]*DEM$female2*DEM$otherparty*DEM$FemaleRepublican+ #FemaleRepublican:female2:otherparty
                               sim_coefs_m1[i,32]*DEM$female2*DEM$rep*1+ #FemaleDemocrat:female2:rep
                               sim_coefs_m1[i,33]*DEM$female2*DEM$rep*DEM$FemaleRepublican #FemaleRepublican:female2:rep
  ))
}
mean(w1.DEM) #


effect.DEM	<- w1.DEM - w0.DEM 
quantile(effect.DEM, c(.025,.975)) #to get CIs; 
mean(effect.DEM) #to get mean effect

#Drop what we no longer need.
rm(DEM, w1.DEM, w0.DEM  )

#Effect of Female Democrats on Democratic Men------
fDmDEM <- subset(sample, dem==1 & female2==0)
w0.fDmDEMm1	<- NULL
w1.fDmDEMm1 	<- NULL

for (i in 1:n_draws) {
  w0.fDmDEMm1[i] <- mean(invlogit(sim_coefs_m1[i,1] + sim_coefs_m1[i,2]*0 + #Female Democrat
                                    sim_coefs_m1[i,3]*fDmDEM$FemaleRepublican + #FemaleRepublican (obs val)
                                    sim_coefs_m1[i,4]*fDmDEM$female2 + 
                                    sim_coefs_m1[i,5]*fDmDEM$GenerationZ +
                                    sim_coefs_m1[i,6]*fDmDEM$Millennials +
                                    sim_coefs_m1[i,7]*fDmDEM$GenerationX +
                                    sim_coefs_m1[i,8]*fDmDEM$SilentGeneration+
                                    sim_coefs_m1[i,9]*fDmDEM$rep +
                                    sim_coefs_m1[i,10]*fDmDEM$otherparty+
                                    sim_coefs_m1[i,11]*fDmDEM$voted2016r+
                                    sim_coefs_m1[i,12]*fDmDEM$Proptrumpvote2016+
                                    sim_coefs_m1[i,13]*fDmDEM$totalreceipts+
                                    sim_coefs_m1[i,14]*fDmDEM$female2*0+ #Female Democrat*Female
                                    sim_coefs_m1[i,15]*fDmDEM$female2*fDmDEM$FemaleRepublican+ #FemaleRepublican:female2
                                    sim_coefs_m1[i,16]*fDmDEM$GenerationZ*0 + #FemaleDemocrat:GenerationZ
                                    sim_coefs_m1[i,17]*fDmDEM$GenerationZ*fDmDEM$FemaleRepublican + #FemaleRepublican:GenerationZ
                                    sim_coefs_m1[i,18]*fDmDEM$Millennials*0 + #FemaleDemocrat:Millennials
                                    sim_coefs_m1[i,19]*fDmDEM$Millennials*fDmDEM$FemaleRepublican + #FemaleRepublican:Millennials
                                    sim_coefs_m1[i,20]*fDmDEM$GenerationX*0 + #FemaleDemocrat:GenerationX
                                    sim_coefs_m1[i,21]*fDmDEM$GenerationX*fDmDEM$FemaleRepublican + #FemaleRepublican:GenerationX
                                    sim_coefs_m1[i,22]*fDmDEM$SilentGeneration*0+ #FemaleDemocrat:SilentGeneration
                                    sim_coefs_m1[i,23]*fDmDEM$SilentGeneration*fDmDEM$FemaleRepublican+ #FemaleRepublican:SilentGeneration
                                    sim_coefs_m1[i,24]*fDmDEM$rep*0 + #FemaleDemocrat:rep
                                    sim_coefs_m1[i,25]*fDmDEM$rep*fDmDEM$FemaleRepublican + #FemaleRepublican:rep
                                    sim_coefs_m1[i,26]*fDmDEM$otherparty*0+ #FemaleDemocrat:otherparty
                                    sim_coefs_m1[i,27]*fDmDEM$otherparty*fDmDEM$FemaleRepublican+ #FemaleRepublican:otherparty
                                    sim_coefs_m1[i,28]*fDmDEM$female2*fDmDEM$rep+ #female2:rep
                                    sim_coefs_m1[i,29]*fDmDEM$female2*fDmDEM$otherparty+ #female2:otherparty
                                    sim_coefs_m1[i,30]*fDmDEM$female2*fDmDEM$otherparty*0+ #FemaleDemocrat:female2:otherparty
                                    sim_coefs_m1[i,31]*fDmDEM$female2*fDmDEM$otherparty*fDmDEM$FemaleRepublican+ #FemaleRepublican:female2:otherparty
                                    sim_coefs_m1[i,32]*fDmDEM$female2*fDmDEM$rep*0+ #FemaleDemocrat:female2:rep
                                    sim_coefs_m1[i,33]*fDmDEM$female2*fDmDEM$rep*fDmDEM$FemaleRepublican #FemaleRepublican:female2:rep
  ))
}
mean(w0.fDmDEMm1) # 


for (i in 1:n_draws) {
  w1.fDmDEMm1[i] <- mean(invlogit(sim_coefs_m1[i,1] + sim_coefs_m1[i,2]*1 + #Female Democrat
                                    sim_coefs_m1[i,3]*fDmDEM$FemaleRepublican + #FemaleRepublican (obs val)
                                    sim_coefs_m1[i,4]*fDmDEM$female2 + 
                                    sim_coefs_m1[i,5]*fDmDEM$GenerationZ +
                                    sim_coefs_m1[i,6]*fDmDEM$Millennials +
                                    sim_coefs_m1[i,7]*fDmDEM$GenerationX +
                                    sim_coefs_m1[i,8]*fDmDEM$SilentGeneration+
                                    sim_coefs_m1[i,9]*fDmDEM$rep +
                                    sim_coefs_m1[i,10]*fDmDEM$otherparty+
                                    sim_coefs_m1[i,11]*fDmDEM$voted2016r+
                                    sim_coefs_m1[i,12]*fDmDEM$Proptrumpvote2016+
                                    sim_coefs_m1[i,13]*fDmDEM$totalreceipts+
                                    sim_coefs_m1[i,14]*fDmDEM$female2*1+ #Female Democrat*Female
                                    sim_coefs_m1[i,15]*fDmDEM$female2*fDmDEM$FemaleRepublican+ #FemaleRepublican:female2
                                    sim_coefs_m1[i,16]*fDmDEM$GenerationZ*1 + #FemaleDemocrat:GenerationZ
                                    sim_coefs_m1[i,17]*fDmDEM$GenerationZ*fDmDEM$FemaleRepublican + #FemaleRepublican:GenerationZ
                                    sim_coefs_m1[i,18]*fDmDEM$Millennials*1 + #FemaleDemocrat:Millennials
                                    sim_coefs_m1[i,19]*fDmDEM$Millennials*fDmDEM$FemaleRepublican + #FemaleRepublican:Millennials
                                    sim_coefs_m1[i,20]*fDmDEM$GenerationX*1 + #FemaleDemocrat:GenerationX
                                    sim_coefs_m1[i,21]*fDmDEM$GenerationX*fDmDEM$FemaleRepublican + #FemaleRepublican:GenerationX
                                    sim_coefs_m1[i,22]*fDmDEM$SilentGeneration*1+ #FemaleDemocrat:SilentGeneration
                                    sim_coefs_m1[i,23]*fDmDEM$SilentGeneration*fDmDEM$FemaleRepublican+ #FemaleRepublican:SilentGeneration
                                    sim_coefs_m1[i,24]*fDmDEM$rep*1 + #FemaleDemocrat:rep
                                    sim_coefs_m1[i,25]*fDmDEM$rep*fDmDEM$FemaleRepublican + #FemaleRepublican:rep
                                    sim_coefs_m1[i,26]*fDmDEM$otherparty*1+ #FemaleDemocrat:otherparty
                                    sim_coefs_m1[i,27]*fDmDEM$otherparty*fDmDEM$FemaleRepublican+ #FemaleRepublican:otherparty
                                    sim_coefs_m1[i,28]*fDmDEM$female2*fDmDEM$rep+ #female2:rep
                                    sim_coefs_m1[i,29]*fDmDEM$female2*fDmDEM$otherparty+ #female2:otherparty
                                    sim_coefs_m1[i,30]*fDmDEM$female2*fDmDEM$otherparty*1+ #FemaleDemocrat:female2:otherparty
                                    sim_coefs_m1[i,31]*fDmDEM$female2*fDmDEM$otherparty*fDmDEM$FemaleRepublican+ #FemaleRepublican:female2:otherparty
                                    sim_coefs_m1[i,32]*fDmDEM$female2*fDmDEM$rep*1+ #FemaleDemocrat:female2:rep
                                    sim_coefs_m1[i,33]*fDmDEM$female2*fDmDEM$rep*fDmDEM$FemaleRepublican #FemaleRepublican:female2:rep
  ))
}
mean(w1.fDmDEMm1) #


effect.fDmDEMm1	<- w1.fDmDEMm1 - w0.fDmDEMm1 
quantile(effect.fDmDEMm1, c(.025,.975)) #to get CIs; 
mean(effect.fDmDEMm1) #to get mean effect

#Drop what we no longer need.
rm(fDmDEM, ww1.fDmDEMm1, w0.fDmDEMm1  )


#Effect of Female Democrats on Democratic Women------
fDmDEM <- subset(sample, dem==1 & female2==1)
w0.fDfDEMm1	<- NULL
w1.fDfDEMm1 	<- NULL

for (i in 1:n_draws) {
  w0.fDfDEMm1[i] <- mean(invlogit(sim_coefs_m1[i,1] + sim_coefs_m1[i,2]*0 + #Female Democrat
                                    sim_coefs_m1[i,3]*fDmDEM$FemaleRepublican + #FemaleRepublican (obs val)
                                    sim_coefs_m1[i,4]*fDmDEM$female2 + 
                                    sim_coefs_m1[i,5]*fDmDEM$GenerationZ +
                                    sim_coefs_m1[i,6]*fDmDEM$Millennials +
                                    sim_coefs_m1[i,7]*fDmDEM$GenerationX +
                                    sim_coefs_m1[i,8]*fDmDEM$SilentGeneration+
                                    sim_coefs_m1[i,9]*fDmDEM$rep +
                                    sim_coefs_m1[i,10]*fDmDEM$otherparty+
                                    sim_coefs_m1[i,11]*fDmDEM$voted2016r+
                                    sim_coefs_m1[i,12]*fDmDEM$Proptrumpvote2016+
                                    sim_coefs_m1[i,13]*fDmDEM$totalreceipts+
                                    sim_coefs_m1[i,14]*fDmDEM$female2*0+ #Female Democrat*Female
                                    sim_coefs_m1[i,15]*fDmDEM$female2*fDmDEM$FemaleRepublican+ #FemaleRepublican:female2
                                    sim_coefs_m1[i,16]*fDmDEM$GenerationZ*0 + #FemaleDemocrat:GenerationZ
                                    sim_coefs_m1[i,17]*fDmDEM$GenerationZ*fDmDEM$FemaleRepublican + #FemaleRepublican:GenerationZ
                                    sim_coefs_m1[i,18]*fDmDEM$Millennials*0 + #FemaleDemocrat:Millennials
                                    sim_coefs_m1[i,19]*fDmDEM$Millennials*fDmDEM$FemaleRepublican + #FemaleRepublican:Millennials
                                    sim_coefs_m1[i,20]*fDmDEM$GenerationX*0 + #FemaleDemocrat:GenerationX
                                    sim_coefs_m1[i,21]*fDmDEM$GenerationX*fDmDEM$FemaleRepublican + #FemaleRepublican:GenerationX
                                    sim_coefs_m1[i,22]*fDmDEM$SilentGeneration*0+ #FemaleDemocrat:SilentGeneration
                                    sim_coefs_m1[i,23]*fDmDEM$SilentGeneration*fDmDEM$FemaleRepublican+ #FemaleRepublican:SilentGeneration
                                    sim_coefs_m1[i,24]*fDmDEM$rep*0 + #FemaleDemocrat:rep
                                    sim_coefs_m1[i,25]*fDmDEM$rep*fDmDEM$FemaleRepublican + #FemaleRepublican:rep
                                    sim_coefs_m1[i,26]*fDmDEM$otherparty*0+ #FemaleDemocrat:otherparty
                                    sim_coefs_m1[i,27]*fDmDEM$otherparty*fDmDEM$FemaleRepublican+ #FemaleRepublican:otherparty
                                    sim_coefs_m1[i,28]*fDmDEM$female2*fDmDEM$rep+ #female2:rep
                                    sim_coefs_m1[i,29]*fDmDEM$female2*fDmDEM$otherparty+ #female2:otherparty
                                    sim_coefs_m1[i,30]*fDmDEM$female2*fDmDEM$otherparty*0+ #FemaleDemocrat:female2:otherparty
                                    sim_coefs_m1[i,31]*fDmDEM$female2*fDmDEM$otherparty*fDmDEM$FemaleRepublican+ #FemaleRepublican:female2:otherparty
                                    sim_coefs_m1[i,32]*fDmDEM$female2*fDmDEM$rep*0+ #FemaleDemocrat:female2:rep
                                    sim_coefs_m1[i,33]*fDmDEM$female2*fDmDEM$rep*fDmDEM$FemaleRepublican #FemaleRepublican:female2:rep
  ))
}
mean(w0.fDfDEMm1) # 


for (i in 1:n_draws) {
  w1.fDfDEMm1[i] <- mean(invlogit(sim_coefs_m1[i,1] + sim_coefs_m1[i,2]*1 + #Female Democrat
                                    sim_coefs_m1[i,3]*fDmDEM$FemaleRepublican + #FemaleRepublican (obs val)
                                    sim_coefs_m1[i,4]*fDmDEM$female2 + 
                                    sim_coefs_m1[i,5]*fDmDEM$GenerationZ +
                                    sim_coefs_m1[i,6]*fDmDEM$Millennials +
                                    sim_coefs_m1[i,7]*fDmDEM$GenerationX +
                                    sim_coefs_m1[i,8]*fDmDEM$SilentGeneration+
                                    sim_coefs_m1[i,9]*fDmDEM$rep +
                                    sim_coefs_m1[i,10]*fDmDEM$otherparty+
                                    sim_coefs_m1[i,11]*fDmDEM$voted2016r+
                                    sim_coefs_m1[i,12]*fDmDEM$Proptrumpvote2016+
                                    sim_coefs_m1[i,13]*fDmDEM$totalreceipts+
                                    sim_coefs_m1[i,14]*fDmDEM$female2*1+ #Female Democrat*Female
                                    sim_coefs_m1[i,15]*fDmDEM$female2*fDmDEM$FemaleRepublican+ #FemaleRepublican:female2
                                    sim_coefs_m1[i,16]*fDmDEM$GenerationZ*1 + #FemaleDemocrat:GenerationZ
                                    sim_coefs_m1[i,17]*fDmDEM$GenerationZ*fDmDEM$FemaleRepublican + #FemaleRepublican:GenerationZ
                                    sim_coefs_m1[i,18]*fDmDEM$Millennials*1 + #FemaleDemocrat:Millennials
                                    sim_coefs_m1[i,19]*fDmDEM$Millennials*fDmDEM$FemaleRepublican + #FemaleRepublican:Millennials
                                    sim_coefs_m1[i,20]*fDmDEM$GenerationX*1 + #FemaleDemocrat:GenerationX
                                    sim_coefs_m1[i,21]*fDmDEM$GenerationX*fDmDEM$FemaleRepublican + #FemaleRepublican:GenerationX
                                    sim_coefs_m1[i,22]*fDmDEM$SilentGeneration*1+ #FemaleDemocrat:SilentGeneration
                                    sim_coefs_m1[i,23]*fDmDEM$SilentGeneration*fDmDEM$FemaleRepublican+ #FemaleRepublican:SilentGeneration
                                    sim_coefs_m1[i,24]*fDmDEM$rep*1 + #FemaleDemocrat:rep
                                    sim_coefs_m1[i,25]*fDmDEM$rep*fDmDEM$FemaleRepublican + #FemaleRepublican:rep
                                    sim_coefs_m1[i,26]*fDmDEM$otherparty*1+ #FemaleDemocrat:otherparty
                                    sim_coefs_m1[i,27]*fDmDEM$otherparty*fDmDEM$FemaleRepublican+ #FemaleRepublican:otherparty
                                    sim_coefs_m1[i,28]*fDmDEM$female2*fDmDEM$rep+ #female2:rep
                                    sim_coefs_m1[i,29]*fDmDEM$female2*fDmDEM$otherparty+ #female2:otherparty
                                    sim_coefs_m1[i,30]*fDmDEM$female2*fDmDEM$otherparty*1+ #FemaleDemocrat:female2:otherparty
                                    sim_coefs_m1[i,31]*fDmDEM$female2*fDmDEM$otherparty*fDmDEM$FemaleRepublican+ #FemaleRepublican:female2:otherparty
                                    sim_coefs_m1[i,32]*fDmDEM$female2*fDmDEM$rep*1+ #FemaleDemocrat:female2:rep
                                    sim_coefs_m1[i,33]*fDmDEM$female2*fDmDEM$rep*fDmDEM$FemaleRepublican #FemaleRepublican:female2:rep
  ))
}
mean(w1.fDfDEMm1) #


effect.fDfDEMm1	<- w1.fDfDEMm1 - w0.fDfDEMm1 
quantile(effect.fDfDEMm1, c(.025,.975)) #to get CIs; 
mean(effect.fDfDEMm1) #to get mean effect

#Drop what we no longer need.
rm(fDmDEM, w1.fDfDEMm1, w0.fDfDEMm1)

#Effect of Female Democrats on Republican Men-------
fDmDEM <- subset(sample, rep==1 & female2==0)
w0.fDmREPm1	<- NULL
w1.fDmREPm1 	<- NULL

for (i in 1:n_draws) {
  w0.fDmREPm1[i] <- mean(invlogit(sim_coefs_m1[i,1] + sim_coefs_m1[i,2]*0 + #Female Democrat
                                    sim_coefs_m1[i,3]*fDmDEM$FemaleRepublican + #FemaleRepublican (obs val)
                                    sim_coefs_m1[i,4]*fDmDEM$female2 + 
                                    sim_coefs_m1[i,5]*fDmDEM$GenerationZ +
                                    sim_coefs_m1[i,6]*fDmDEM$Millennials +
                                    sim_coefs_m1[i,7]*fDmDEM$GenerationX +
                                    sim_coefs_m1[i,8]*fDmDEM$SilentGeneration+
                                    sim_coefs_m1[i,9]*fDmDEM$rep +
                                    sim_coefs_m1[i,10]*fDmDEM$otherparty+
                                    sim_coefs_m1[i,11]*fDmDEM$voted2016r+
                                    sim_coefs_m1[i,12]*fDmDEM$Proptrumpvote2016+
                                    sim_coefs_m1[i,13]*fDmDEM$totalreceipts+
                                    sim_coefs_m1[i,14]*fDmDEM$female2*0+ #Female Democrat*Female
                                    sim_coefs_m1[i,15]*fDmDEM$female2*fDmDEM$FemaleRepublican+ #FemaleRepublican:female2
                                    sim_coefs_m1[i,16]*fDmDEM$GenerationZ*0 + #FemaleDemocrat:GenerationZ
                                    sim_coefs_m1[i,17]*fDmDEM$GenerationZ*fDmDEM$FemaleRepublican + #FemaleRepublican:GenerationZ
                                    sim_coefs_m1[i,18]*fDmDEM$Millennials*0 + #FemaleDemocrat:Millennials
                                    sim_coefs_m1[i,19]*fDmDEM$Millennials*fDmDEM$FemaleRepublican + #FemaleRepublican:Millennials
                                    sim_coefs_m1[i,20]*fDmDEM$GenerationX*0 + #FemaleDemocrat:GenerationX
                                    sim_coefs_m1[i,21]*fDmDEM$GenerationX*fDmDEM$FemaleRepublican + #FemaleRepublican:GenerationX
                                    sim_coefs_m1[i,22]*fDmDEM$SilentGeneration*0+ #FemaleDemocrat:SilentGeneration
                                    sim_coefs_m1[i,23]*fDmDEM$SilentGeneration*fDmDEM$FemaleRepublican+ #FemaleRepublican:SilentGeneration
                                    sim_coefs_m1[i,24]*fDmDEM$rep*0 + #FemaleDemocrat:rep
                                    sim_coefs_m1[i,25]*fDmDEM$rep*fDmDEM$FemaleRepublican + #FemaleRepublican:rep
                                    sim_coefs_m1[i,26]*fDmDEM$otherparty*0+ #FemaleDemocrat:otherparty
                                    sim_coefs_m1[i,27]*fDmDEM$otherparty*fDmDEM$FemaleRepublican+ #FemaleRepublican:otherparty
                                    sim_coefs_m1[i,28]*fDmDEM$female2*fDmDEM$rep+ #female2:rep
                                    sim_coefs_m1[i,29]*fDmDEM$female2*fDmDEM$otherparty+ #female2:otherparty
                                    sim_coefs_m1[i,30]*fDmDEM$female2*fDmDEM$otherparty*0+ #FemaleDemocrat:female2:otherparty
                                    sim_coefs_m1[i,31]*fDmDEM$female2*fDmDEM$otherparty*fDmDEM$FemaleRepublican+ #FemaleRepublican:female2:otherparty
                                    sim_coefs_m1[i,32]*fDmDEM$female2*fDmDEM$rep*0+ #FemaleDemocrat:female2:rep
                                    sim_coefs_m1[i,33]*fDmDEM$female2*fDmDEM$rep*fDmDEM$FemaleRepublican #FemaleRepublican:female2:rep
  ))
}
mean(w0.fDmREPm1) # 


for (i in 1:n_draws) {
  w1.fDmREPm1[i] <- mean(invlogit(sim_coefs_m1[i,1] + sim_coefs_m1[i,2]*1 + #Female Democrat
                                    sim_coefs_m1[i,3]*fDmDEM$FemaleRepublican + #FemaleRepublican (obs val)
                                    sim_coefs_m1[i,4]*fDmDEM$female2 + 
                                    sim_coefs_m1[i,5]*fDmDEM$GenerationZ +
                                    sim_coefs_m1[i,6]*fDmDEM$Millennials +
                                    sim_coefs_m1[i,7]*fDmDEM$GenerationX +
                                    sim_coefs_m1[i,8]*fDmDEM$SilentGeneration+
                                    sim_coefs_m1[i,9]*fDmDEM$rep +
                                    sim_coefs_m1[i,10]*fDmDEM$otherparty+
                                    sim_coefs_m1[i,11]*fDmDEM$voted2016r+
                                    sim_coefs_m1[i,12]*fDmDEM$Proptrumpvote2016+
                                    sim_coefs_m1[i,13]*fDmDEM$totalreceipts+
                                    sim_coefs_m1[i,14]*fDmDEM$female2*1+ #Female Democrat*Female
                                    sim_coefs_m1[i,15]*fDmDEM$female2*fDmDEM$FemaleRepublican+ #FemaleRepublican:female2
                                    sim_coefs_m1[i,16]*fDmDEM$GenerationZ*1 + #FemaleDemocrat:GenerationZ
                                    sim_coefs_m1[i,17]*fDmDEM$GenerationZ*fDmDEM$FemaleRepublican + #FemaleRepublican:GenerationZ
                                    sim_coefs_m1[i,18]*fDmDEM$Millennials*1 + #FemaleDemocrat:Millennials
                                    sim_coefs_m1[i,19]*fDmDEM$Millennials*fDmDEM$FemaleRepublican + #FemaleRepublican:Millennials
                                    sim_coefs_m1[i,20]*fDmDEM$GenerationX*1 + #FemaleDemocrat:GenerationX
                                    sim_coefs_m1[i,21]*fDmDEM$GenerationX*fDmDEM$FemaleRepublican + #FemaleRepublican:GenerationX
                                    sim_coefs_m1[i,22]*fDmDEM$SilentGeneration*1+ #FemaleDemocrat:SilentGeneration
                                    sim_coefs_m1[i,23]*fDmDEM$SilentGeneration*fDmDEM$FemaleRepublican+ #FemaleRepublican:SilentGeneration
                                    sim_coefs_m1[i,24]*fDmDEM$rep*1 + #FemaleDemocrat:rep
                                    sim_coefs_m1[i,25]*fDmDEM$rep*fDmDEM$FemaleRepublican + #FemaleRepublican:rep
                                    sim_coefs_m1[i,26]*fDmDEM$otherparty*1+ #FemaleDemocrat:otherparty
                                    sim_coefs_m1[i,27]*fDmDEM$otherparty*fDmDEM$FemaleRepublican+ #FemaleRepublican:otherparty
                                    sim_coefs_m1[i,28]*fDmDEM$female2*fDmDEM$rep+ #female2:rep
                                    sim_coefs_m1[i,29]*fDmDEM$female2*fDmDEM$otherparty+ #female2:otherparty
                                    sim_coefs_m1[i,30]*fDmDEM$female2*fDmDEM$otherparty*1+ #FemaleDemocrat:female2:otherparty
                                    sim_coefs_m1[i,31]*fDmDEM$female2*fDmDEM$otherparty*fDmDEM$FemaleRepublican+ #FemaleRepublican:female2:otherparty
                                    sim_coefs_m1[i,32]*fDmDEM$female2*fDmDEM$rep*1+ #FemaleDemocrat:female2:rep
                                    sim_coefs_m1[i,33]*fDmDEM$female2*fDmDEM$rep*fDmDEM$FemaleRepublican #FemaleRepublican:female2:rep
  ))
}
mean(w1.fDmREPm1) #


effect.fDmREPm1	<- w1.fDmREPm1 - w0.fDmREPm1 
quantile(effect.fDmREPm1, c(.025,.975)) #to get CIs; 
mean(effect.fDmREPm1) #to get mean effect

#Drop what we no longer need.
rm(fDmDEM, w1.fDmREPm1, w0.fDmREPm1)

#Effect of Female Democrats on Republican Women------
fDmDEM <- subset(sample, rep==1 & female2==1)
w0.fDfREPm1	<- NULL
w1.fDfREPm1 	<- NULL

for (i in 1:n_draws) {
  w0.fDfREPm1[i] <- mean(invlogit(sim_coefs_m1[i,1] + sim_coefs_m1[i,2]*0 + #Female Democrat
                                    sim_coefs_m1[i,3]*fDmDEM$FemaleRepublican + #FemaleRepublican (obs val)
                                    sim_coefs_m1[i,4]*fDmDEM$female2 + 
                                    sim_coefs_m1[i,5]*fDmDEM$GenerationZ +
                                    sim_coefs_m1[i,6]*fDmDEM$Millennials +
                                    sim_coefs_m1[i,7]*fDmDEM$GenerationX +
                                    sim_coefs_m1[i,8]*fDmDEM$SilentGeneration+
                                    sim_coefs_m1[i,9]*fDmDEM$rep +
                                    sim_coefs_m1[i,10]*fDmDEM$otherparty+
                                    sim_coefs_m1[i,11]*fDmDEM$voted2016r+
                                    sim_coefs_m1[i,12]*fDmDEM$Proptrumpvote2016+
                                    sim_coefs_m1[i,13]*fDmDEM$totalreceipts+
                                    sim_coefs_m1[i,14]*fDmDEM$female2*0+ #Female Democrat*Female
                                    sim_coefs_m1[i,15]*fDmDEM$female2*fDmDEM$FemaleRepublican+ #FemaleRepublican:female2
                                    sim_coefs_m1[i,16]*fDmDEM$GenerationZ*0 + #FemaleDemocrat:GenerationZ
                                    sim_coefs_m1[i,17]*fDmDEM$GenerationZ*fDmDEM$FemaleRepublican + #FemaleRepublican:GenerationZ
                                    sim_coefs_m1[i,18]*fDmDEM$Millennials*0 + #FemaleDemocrat:Millennials
                                    sim_coefs_m1[i,19]*fDmDEM$Millennials*fDmDEM$FemaleRepublican + #FemaleRepublican:Millennials
                                    sim_coefs_m1[i,20]*fDmDEM$GenerationX*0 + #FemaleDemocrat:GenerationX
                                    sim_coefs_m1[i,21]*fDmDEM$GenerationX*fDmDEM$FemaleRepublican + #FemaleRepublican:GenerationX
                                    sim_coefs_m1[i,22]*fDmDEM$SilentGeneration*0+ #FemaleDemocrat:SilentGeneration
                                    sim_coefs_m1[i,23]*fDmDEM$SilentGeneration*fDmDEM$FemaleRepublican+ #FemaleRepublican:SilentGeneration
                                    sim_coefs_m1[i,24]*fDmDEM$rep*0 + #FemaleDemocrat:rep
                                    sim_coefs_m1[i,25]*fDmDEM$rep*fDmDEM$FemaleRepublican + #FemaleRepublican:rep
                                    sim_coefs_m1[i,26]*fDmDEM$otherparty*0+ #FemaleDemocrat:otherparty
                                    sim_coefs_m1[i,27]*fDmDEM$otherparty*fDmDEM$FemaleRepublican+ #FemaleRepublican:otherparty
                                    sim_coefs_m1[i,28]*fDmDEM$female2*fDmDEM$rep+ #female2:rep
                                    sim_coefs_m1[i,29]*fDmDEM$female2*fDmDEM$otherparty+ #female2:otherparty
                                    sim_coefs_m1[i,30]*fDmDEM$female2*fDmDEM$otherparty*0+ #FemaleDemocrat:female2:otherparty
                                    sim_coefs_m1[i,31]*fDmDEM$female2*fDmDEM$otherparty*fDmDEM$FemaleRepublican+ #FemaleRepublican:female2:otherparty
                                    sim_coefs_m1[i,32]*fDmDEM$female2*fDmDEM$rep*0+ #FemaleDemocrat:female2:rep
                                    sim_coefs_m1[i,33]*fDmDEM$female2*fDmDEM$rep*fDmDEM$FemaleRepublican #FemaleRepublican:female2:rep
  ))
}
mean(w0.fDfREPm1) # 


for (i in 1:n_draws) {
  w1.fDfREPm1[i] <- mean(invlogit(sim_coefs_m1[i,1] + sim_coefs_m1[i,2]*1 + #Female Democrat
                                    sim_coefs_m1[i,3]*fDmDEM$FemaleRepublican + #FemaleRepublican (obs val)
                                    sim_coefs_m1[i,4]*fDmDEM$female2 + 
                                    sim_coefs_m1[i,5]*fDmDEM$GenerationZ +
                                    sim_coefs_m1[i,6]*fDmDEM$Millennials +
                                    sim_coefs_m1[i,7]*fDmDEM$GenerationX +
                                    sim_coefs_m1[i,8]*fDmDEM$SilentGeneration+
                                    sim_coefs_m1[i,9]*fDmDEM$rep +
                                    sim_coefs_m1[i,10]*fDmDEM$otherparty+
                                    sim_coefs_m1[i,11]*fDmDEM$voted2016r+
                                    sim_coefs_m1[i,12]*fDmDEM$Proptrumpvote2016+
                                    sim_coefs_m1[i,13]*fDmDEM$totalreceipts+
                                    sim_coefs_m1[i,14]*fDmDEM$female2*1+ #Female Democrat*Female
                                    sim_coefs_m1[i,15]*fDmDEM$female2*fDmDEM$FemaleRepublican+ #FemaleRepublican:female2
                                    sim_coefs_m1[i,16]*fDmDEM$GenerationZ*1 + #FemaleDemocrat:GenerationZ
                                    sim_coefs_m1[i,17]*fDmDEM$GenerationZ*fDmDEM$FemaleRepublican + #FemaleRepublican:GenerationZ
                                    sim_coefs_m1[i,18]*fDmDEM$Millennials*1 + #FemaleDemocrat:Millennials
                                    sim_coefs_m1[i,19]*fDmDEM$Millennials*fDmDEM$FemaleRepublican + #FemaleRepublican:Millennials
                                    sim_coefs_m1[i,20]*fDmDEM$GenerationX*1 + #FemaleDemocrat:GenerationX
                                    sim_coefs_m1[i,21]*fDmDEM$GenerationX*fDmDEM$FemaleRepublican + #FemaleRepublican:GenerationX
                                    sim_coefs_m1[i,22]*fDmDEM$SilentGeneration*1+ #FemaleDemocrat:SilentGeneration
                                    sim_coefs_m1[i,23]*fDmDEM$SilentGeneration*fDmDEM$FemaleRepublican+ #FemaleRepublican:SilentGeneration
                                    sim_coefs_m1[i,24]*fDmDEM$rep*1 + #FemaleDemocrat:rep
                                    sim_coefs_m1[i,25]*fDmDEM$rep*fDmDEM$FemaleRepublican + #FemaleRepublican:rep
                                    sim_coefs_m1[i,26]*fDmDEM$otherparty*1+ #FemaleDemocrat:otherparty
                                    sim_coefs_m1[i,27]*fDmDEM$otherparty*fDmDEM$FemaleRepublican+ #FemaleRepublican:otherparty
                                    sim_coefs_m1[i,28]*fDmDEM$female2*fDmDEM$rep+ #female2:rep
                                    sim_coefs_m1[i,29]*fDmDEM$female2*fDmDEM$otherparty+ #female2:otherparty
                                    sim_coefs_m1[i,30]*fDmDEM$female2*fDmDEM$otherparty*1+ #FemaleDemocrat:female2:otherparty
                                    sim_coefs_m1[i,31]*fDmDEM$female2*fDmDEM$otherparty*fDmDEM$FemaleRepublican+ #FemaleRepublican:female2:otherparty
                                    sim_coefs_m1[i,32]*fDmDEM$female2*fDmDEM$rep*1+ #FemaleDemocrat:female2:rep
                                    sim_coefs_m1[i,33]*fDmDEM$female2*fDmDEM$rep*fDmDEM$FemaleRepublican #FemaleRepublican:female2:rep
  ))
}
mean(w1.fDfREPm1) #

effect.fDfREPm1	<- w1.fDfREPm1 - w0.fDfREPm1 
quantile(effect.fDfREPm1, c(.025,.975)) #to get CIs; 
mean(effect.fDfREPm1) #to get mean effect

#Drop what we no longer need.
rm(fDmDEM, w1.fDfREPm1, w0.fDfREPm1)

#Effect of Female Democrats on Other Party Men-------
fDmDEM <- subset(sample, otherparty==1 & female2==0)
w0.fDmOPm1	<- NULL
w1.fDmOPm1 	<- NULL

for (i in 1:n_draws) {
  w0.fDmOPm1[i] <- mean(invlogit(sim_coefs_m1[i,1] + sim_coefs_m1[i,2]*0 + #Female Democrat
                                   sim_coefs_m1[i,3]*fDmDEM$FemaleRepublican + #FemaleRepublican (obs val)
                                   sim_coefs_m1[i,4]*fDmDEM$female2 + 
                                   sim_coefs_m1[i,5]*fDmDEM$GenerationZ +
                                   sim_coefs_m1[i,6]*fDmDEM$Millennials +
                                   sim_coefs_m1[i,7]*fDmDEM$GenerationX +
                                   sim_coefs_m1[i,8]*fDmDEM$SilentGeneration+
                                   sim_coefs_m1[i,9]*fDmDEM$rep +
                                   sim_coefs_m1[i,10]*fDmDEM$otherparty+
                                   sim_coefs_m1[i,11]*fDmDEM$voted2016r+
                                   sim_coefs_m1[i,12]*fDmDEM$Proptrumpvote2016+
                                   sim_coefs_m1[i,13]*fDmDEM$totalreceipts+
                                   sim_coefs_m1[i,14]*fDmDEM$female2*0+ #Female Democrat*Female
                                   sim_coefs_m1[i,15]*fDmDEM$female2*fDmDEM$FemaleRepublican+ #FemaleRepublican:female2
                                   sim_coefs_m1[i,16]*fDmDEM$GenerationZ*0 + #FemaleDemocrat:GenerationZ
                                   sim_coefs_m1[i,17]*fDmDEM$GenerationZ*fDmDEM$FemaleRepublican + #FemaleRepublican:GenerationZ
                                   sim_coefs_m1[i,18]*fDmDEM$Millennials*0 + #FemaleDemocrat:Millennials
                                   sim_coefs_m1[i,19]*fDmDEM$Millennials*fDmDEM$FemaleRepublican + #FemaleRepublican:Millennials
                                   sim_coefs_m1[i,20]*fDmDEM$GenerationX*0 + #FemaleDemocrat:GenerationX
                                   sim_coefs_m1[i,21]*fDmDEM$GenerationX*fDmDEM$FemaleRepublican + #FemaleRepublican:GenerationX
                                   sim_coefs_m1[i,22]*fDmDEM$SilentGeneration*0+ #FemaleDemocrat:SilentGeneration
                                   sim_coefs_m1[i,23]*fDmDEM$SilentGeneration*fDmDEM$FemaleRepublican+ #FemaleRepublican:SilentGeneration
                                   sim_coefs_m1[i,24]*fDmDEM$rep*0 + #FemaleDemocrat:rep
                                   sim_coefs_m1[i,25]*fDmDEM$rep*fDmDEM$FemaleRepublican + #FemaleRepublican:rep
                                   sim_coefs_m1[i,26]*fDmDEM$otherparty*0+ #FemaleDemocrat:otherparty
                                   sim_coefs_m1[i,27]*fDmDEM$otherparty*fDmDEM$FemaleRepublican+ #FemaleRepublican:otherparty
                                   sim_coefs_m1[i,28]*fDmDEM$female2*fDmDEM$rep+ #female2:rep
                                   sim_coefs_m1[i,29]*fDmDEM$female2*fDmDEM$otherparty+ #female2:otherparty
                                   sim_coefs_m1[i,30]*fDmDEM$female2*fDmDEM$otherparty*0+ #FemaleDemocrat:female2:otherparty
                                   sim_coefs_m1[i,31]*fDmDEM$female2*fDmDEM$otherparty*fDmDEM$FemaleRepublican+ #FemaleRepublican:female2:otherparty
                                   sim_coefs_m1[i,32]*fDmDEM$female2*fDmDEM$rep*0+ #FemaleDemocrat:female2:rep
                                   sim_coefs_m1[i,33]*fDmDEM$female2*fDmDEM$rep*fDmDEM$FemaleRepublican #FemaleRepublican:female2:rep
  ))
}
mean(w0.fDmOPm1) # 


for (i in 1:n_draws) {
  w1.fDmOPm1[i] <- mean(invlogit(sim_coefs_m1[i,1] + sim_coefs_m1[i,2]*1 + #Female Democrat
                                   sim_coefs_m1[i,3]*fDmDEM$FemaleRepublican + #FemaleRepublican (obs val)
                                   sim_coefs_m1[i,4]*fDmDEM$female2 + 
                                   sim_coefs_m1[i,5]*fDmDEM$GenerationZ +
                                   sim_coefs_m1[i,6]*fDmDEM$Millennials +
                                   sim_coefs_m1[i,7]*fDmDEM$GenerationX +
                                   sim_coefs_m1[i,8]*fDmDEM$SilentGeneration+
                                   sim_coefs_m1[i,9]*fDmDEM$rep +
                                   sim_coefs_m1[i,10]*fDmDEM$otherparty+
                                   sim_coefs_m1[i,11]*fDmDEM$voted2016r+
                                   sim_coefs_m1[i,12]*fDmDEM$Proptrumpvote2016+
                                   sim_coefs_m1[i,13]*fDmDEM$totalreceipts+
                                   sim_coefs_m1[i,14]*fDmDEM$female2*1+ #Female Democrat*Female
                                   sim_coefs_m1[i,15]*fDmDEM$female2*fDmDEM$FemaleRepublican+ #FemaleRepublican:female2
                                   sim_coefs_m1[i,16]*fDmDEM$GenerationZ*1 + #FemaleDemocrat:GenerationZ
                                   sim_coefs_m1[i,17]*fDmDEM$GenerationZ*fDmDEM$FemaleRepublican + #FemaleRepublican:GenerationZ
                                   sim_coefs_m1[i,18]*fDmDEM$Millennials*1 + #FemaleDemocrat:Millennials
                                   sim_coefs_m1[i,19]*fDmDEM$Millennials*fDmDEM$FemaleRepublican + #FemaleRepublican:Millennials
                                   sim_coefs_m1[i,20]*fDmDEM$GenerationX*1 + #FemaleDemocrat:GenerationX
                                   sim_coefs_m1[i,21]*fDmDEM$GenerationX*fDmDEM$FemaleRepublican + #FemaleRepublican:GenerationX
                                   sim_coefs_m1[i,22]*fDmDEM$SilentGeneration*1+ #FemaleDemocrat:SilentGeneration
                                   sim_coefs_m1[i,23]*fDmDEM$SilentGeneration*fDmDEM$FemaleRepublican+ #FemaleRepublican:SilentGeneration
                                   sim_coefs_m1[i,24]*fDmDEM$rep*1 + #FemaleDemocrat:rep
                                   sim_coefs_m1[i,25]*fDmDEM$rep*fDmDEM$FemaleRepublican + #FemaleRepublican:rep
                                   sim_coefs_m1[i,26]*fDmDEM$otherparty*1+ #FemaleDemocrat:otherparty
                                   sim_coefs_m1[i,27]*fDmDEM$otherparty*fDmDEM$FemaleRepublican+ #FemaleRepublican:otherparty
                                   sim_coefs_m1[i,28]*fDmDEM$female2*fDmDEM$rep+ #female2:rep
                                   sim_coefs_m1[i,29]*fDmDEM$female2*fDmDEM$otherparty+ #female2:otherparty
                                   sim_coefs_m1[i,30]*fDmDEM$female2*fDmDEM$otherparty*1+ #FemaleDemocrat:female2:otherparty
                                   sim_coefs_m1[i,31]*fDmDEM$female2*fDmDEM$otherparty*fDmDEM$FemaleRepublican+ #FemaleRepublican:female2:otherparty
                                   sim_coefs_m1[i,32]*fDmDEM$female2*fDmDEM$rep*1+ #FemaleDemocrat:female2:rep
                                   sim_coefs_m1[i,33]*fDmDEM$female2*fDmDEM$rep*fDmDEM$FemaleRepublican #FemaleRepublican:female2:rep
  ))
}
mean(w1.fDmOPm1) #

effect.fDmOPm1	<- w1.fDmOPm1 - w0.fDmOPm1 
quantile(effect.fDmOPm1, c(.025,.975)) #to get CIs; 
mean(effect.fDmOPm1) #to get mean effect

#Drop what we no longer need.
rm(fDmDEM, w1.fDmOPm1, w0.fDmOPm1)

#Effect of Female Democrats on Other Party Women------
fDmDEM <- subset(sample, otherparty==1 & female2==1)
w0.fDfOPm1	<- NULL
w1.fDfOPm1 	<- NULL

for (i in 1:n_draws) {
  w0.fDfOPm1[i] <- mean(invlogit(sim_coefs_m1[i,1] + sim_coefs_m1[i,2]*0 + #Female Democrat
                                   sim_coefs_m1[i,3]*fDmDEM$FemaleRepublican + #FemaleRepublican (obs val)
                                   sim_coefs_m1[i,4]*fDmDEM$female2 + 
                                   sim_coefs_m1[i,5]*fDmDEM$GenerationZ +
                                   sim_coefs_m1[i,6]*fDmDEM$Millennials +
                                   sim_coefs_m1[i,7]*fDmDEM$GenerationX +
                                   sim_coefs_m1[i,8]*fDmDEM$SilentGeneration+
                                   sim_coefs_m1[i,9]*fDmDEM$rep +
                                   sim_coefs_m1[i,10]*fDmDEM$otherparty+
                                   sim_coefs_m1[i,11]*fDmDEM$voted2016r+
                                   sim_coefs_m1[i,12]*fDmDEM$Proptrumpvote2016+
                                   sim_coefs_m1[i,13]*fDmDEM$totalreceipts+
                                   sim_coefs_m1[i,14]*fDmDEM$female2*0+ #Female Democrat*Female
                                   sim_coefs_m1[i,15]*fDmDEM$female2*fDmDEM$FemaleRepublican+ #FemaleRepublican:female2
                                   sim_coefs_m1[i,16]*fDmDEM$GenerationZ*0 + #FemaleDemocrat:GenerationZ
                                   sim_coefs_m1[i,17]*fDmDEM$GenerationZ*fDmDEM$FemaleRepublican + #FemaleRepublican:GenerationZ
                                   sim_coefs_m1[i,18]*fDmDEM$Millennials*0 + #FemaleDemocrat:Millennials
                                   sim_coefs_m1[i,19]*fDmDEM$Millennials*fDmDEM$FemaleRepublican + #FemaleRepublican:Millennials
                                   sim_coefs_m1[i,20]*fDmDEM$GenerationX*0 + #FemaleDemocrat:GenerationX
                                   sim_coefs_m1[i,21]*fDmDEM$GenerationX*fDmDEM$FemaleRepublican + #FemaleRepublican:GenerationX
                                   sim_coefs_m1[i,22]*fDmDEM$SilentGeneration*0+ #FemaleDemocrat:SilentGeneration
                                   sim_coefs_m1[i,23]*fDmDEM$SilentGeneration*fDmDEM$FemaleRepublican+ #FemaleRepublican:SilentGeneration
                                   sim_coefs_m1[i,24]*fDmDEM$rep*0 + #FemaleDemocrat:rep
                                   sim_coefs_m1[i,25]*fDmDEM$rep*fDmDEM$FemaleRepublican + #FemaleRepublican:rep
                                   sim_coefs_m1[i,26]*fDmDEM$otherparty*0+ #FemaleDemocrat:otherparty
                                   sim_coefs_m1[i,27]*fDmDEM$otherparty*fDmDEM$FemaleRepublican+ #FemaleRepublican:otherparty
                                   sim_coefs_m1[i,28]*fDmDEM$female2*fDmDEM$rep+ #female2:rep
                                   sim_coefs_m1[i,29]*fDmDEM$female2*fDmDEM$otherparty+ #female2:otherparty
                                   sim_coefs_m1[i,30]*fDmDEM$female2*fDmDEM$otherparty*0+ #FemaleDemocrat:female2:otherparty
                                   sim_coefs_m1[i,31]*fDmDEM$female2*fDmDEM$otherparty*fDmDEM$FemaleRepublican+ #FemaleRepublican:female2:otherparty
                                   sim_coefs_m1[i,32]*fDmDEM$female2*fDmDEM$rep*0+ #FemaleDemocrat:female2:rep
                                   sim_coefs_m1[i,33]*fDmDEM$female2*fDmDEM$rep*fDmDEM$FemaleRepublican #FemaleRepublican:female2:rep
  ))
}
mean(w0.fDfOPm1) # 


for (i in 1:n_draws) {
  w1.fDfOPm1[i] <- mean(invlogit(sim_coefs_m1[i,1] + sim_coefs_m1[i,2]*1 + #Female Democrat
                                   sim_coefs_m1[i,3]*fDmDEM$FemaleRepublican + #FemaleRepublican (obs val)
                                   sim_coefs_m1[i,4]*fDmDEM$female2 + 
                                   sim_coefs_m1[i,5]*fDmDEM$GenerationZ +
                                   sim_coefs_m1[i,6]*fDmDEM$Millennials +
                                   sim_coefs_m1[i,7]*fDmDEM$GenerationX +
                                   sim_coefs_m1[i,8]*fDmDEM$SilentGeneration+
                                   sim_coefs_m1[i,9]*fDmDEM$rep +
                                   sim_coefs_m1[i,10]*fDmDEM$otherparty+
                                   sim_coefs_m1[i,11]*fDmDEM$voted2016r+
                                   sim_coefs_m1[i,12]*fDmDEM$Proptrumpvote2016+
                                   sim_coefs_m1[i,13]*fDmDEM$totalreceipts+
                                   sim_coefs_m1[i,14]*fDmDEM$female2*1+ #Female Democrat*Female
                                   sim_coefs_m1[i,15]*fDmDEM$female2*fDmDEM$FemaleRepublican+ #FemaleRepublican:female2
                                   sim_coefs_m1[i,16]*fDmDEM$GenerationZ*1 + #FemaleDemocrat:GenerationZ
                                   sim_coefs_m1[i,17]*fDmDEM$GenerationZ*fDmDEM$FemaleRepublican + #FemaleRepublican:GenerationZ
                                   sim_coefs_m1[i,18]*fDmDEM$Millennials*1 + #FemaleDemocrat:Millennials
                                   sim_coefs_m1[i,19]*fDmDEM$Millennials*fDmDEM$FemaleRepublican + #FemaleRepublican:Millennials
                                   sim_coefs_m1[i,20]*fDmDEM$GenerationX*1 + #FemaleDemocrat:GenerationX
                                   sim_coefs_m1[i,21]*fDmDEM$GenerationX*fDmDEM$FemaleRepublican + #FemaleRepublican:GenerationX
                                   sim_coefs_m1[i,22]*fDmDEM$SilentGeneration*1+ #FemaleDemocrat:SilentGeneration
                                   sim_coefs_m1[i,23]*fDmDEM$SilentGeneration*fDmDEM$FemaleRepublican+ #FemaleRepublican:SilentGeneration
                                   sim_coefs_m1[i,24]*fDmDEM$rep*1 + #FemaleDemocrat:rep
                                   sim_coefs_m1[i,25]*fDmDEM$rep*fDmDEM$FemaleRepublican + #FemaleRepublican:rep
                                   sim_coefs_m1[i,26]*fDmDEM$otherparty*1+ #FemaleDemocrat:otherparty
                                   sim_coefs_m1[i,27]*fDmDEM$otherparty*fDmDEM$FemaleRepublican+ #FemaleRepublican:otherparty
                                   sim_coefs_m1[i,28]*fDmDEM$female2*fDmDEM$rep+ #female2:rep
                                   sim_coefs_m1[i,29]*fDmDEM$female2*fDmDEM$otherparty+ #female2:otherparty
                                   sim_coefs_m1[i,30]*fDmDEM$female2*fDmDEM$otherparty*1+ #FemaleDemocrat:female2:otherparty
                                   sim_coefs_m1[i,31]*fDmDEM$female2*fDmDEM$otherparty*fDmDEM$FemaleRepublican+ #FemaleRepublican:female2:otherparty
                                   sim_coefs_m1[i,32]*fDmDEM$female2*fDmDEM$rep*1+ #FemaleDemocrat:female2:rep
                                   sim_coefs_m1[i,33]*fDmDEM$female2*fDmDEM$rep*fDmDEM$FemaleRepublican #FemaleRepublican:female2:rep
  ))
}
mean(w1.fDfOPm1) #

effect.fDfOPm1	<- w1.fDfOPm1 - w0.fDfOPm1 
quantile(effect.fDfOPm1, c(.025,.975)) #to get CIs; 
mean(effect.fDfOPm1) #to get mean effect

#Drop what we no longer need.
rm(fDmDEM, w1.fDfOPm1, w0.fDfOPm1 )

#Effect of Female Republicans on Republican Women-----
fRfR <- subset(sample, rep==1 & female2==1)
w0.fRfRm1	<- NULL
w1.fRfRm1 	<- NULL


for (i in 1:n_draws) {
  w0.fRfRm1[i] <- mean(invlogit(sim_coefs_m1[i,1] + sim_coefs_m1[i,2]*fRfR$FemaleDemocrat + #Female Democrat (obs val)
                                  sim_coefs_m1[i,3]*0 + #FemaleRepublican 
                                  sim_coefs_m1[i,4]*fRfR$female2 + 
                                  sim_coefs_m1[i,5]*fRfR$GenerationZ +
                                  sim_coefs_m1[i,6]*fRfR$Millennials +
                                  sim_coefs_m1[i,7]*fRfR$GenerationX +
                                  sim_coefs_m1[i,8]*fRfR$SilentGeneration+
                                  sim_coefs_m1[i,9]*fRfR$rep +
                                  sim_coefs_m1[i,10]*fRfR$otherparty+
                                  sim_coefs_m1[i,11]*fRfR$voted2016r+
                                  sim_coefs_m1[i,12]*fRfR$Proptrumpvote2016+
                                  sim_coefs_m1[i,13]*fRfR$totalreceipts+
                                  sim_coefs_m1[i,14]*fRfR$female2*fRfR$FemaleDemocrat+ #Female Democrat*Female
                                  sim_coefs_m1[i,15]*fRfR$female2*0+ #FemaleRepublican:female2
                                  sim_coefs_m1[i,16]*fRfR$GenerationZ*fRfR$FemaleDemocrat + #FemaleDemocrat:GenerationZ
                                  sim_coefs_m1[i,17]*fRfR$GenerationZ*0 + #FemaleRepublican:GenerationZ
                                  sim_coefs_m1[i,18]*fRfR$Millennials*fRfR$FemaleDemocrat + #FemaleDemocrat:Millennials
                                  sim_coefs_m1[i,19]*fRfR$Millennials*0 + #FemaleRepublican:Millennials
                                  sim_coefs_m1[i,20]*fRfR$GenerationX*fRfR$FemaleDemocrat + #FemaleDemocrat:GenerationX
                                  sim_coefs_m1[i,21]*fRfR$GenerationX*0 + #FemaleRepublican:GenerationX
                                  sim_coefs_m1[i,22]*fRfR$SilentGeneration*fRfR$FemaleDemocrat+ #FemaleDemocrat:SilentGeneration
                                  sim_coefs_m1[i,23]*fRfR$SilentGeneration*0+ #FemaleRepublican:SilentGeneration
                                  sim_coefs_m1[i,24]*fRfR$rep*fRfR$FemaleDemocrat + #FemaleDemocrat:rep
                                  sim_coefs_m1[i,25]*fRfR$rep*0 + #FemaleRepublican:rep
                                  sim_coefs_m1[i,26]*fRfR$otherparty*fRfR$FemaleDemocrat+ #FemaleDemocrat:otherparty
                                  sim_coefs_m1[i,27]*fRfR$otherparty*0+ #FemaleRepublican:otherparty
                                  sim_coefs_m1[i,28]*fRfR$female2*fRfR$rep+ #female2:rep
                                  sim_coefs_m1[i,29]*fRfR$female2*fRfR$otherparty+ #female2:otherparty
                                  sim_coefs_m1[i,30]*fRfR$female2*fRfR$otherparty*fRfR$FemaleDemocrat+ #FemaleDemocrat:female2:otherparty
                                  sim_coefs_m1[i,31]*fRfR$female2*fRfR$otherparty*0+ #FemaleRepublican:female2:otherparty
                                  sim_coefs_m1[i,32]*fRfR$female2*fRfR$rep*fRfR$FemaleDemocrat+ #FemaleDemocrat:female2:rep
                                  sim_coefs_m1[i,33]*fRfR$female2*fRfR$rep*0 #FemaleRepublican:female2:rep
  ))
}
mean(w0.fRfRm1) # 


for (i in 1:n_draws) {
  w1.fRfRm1[i] <- mean(invlogit(sim_coefs_m1[i,1] + sim_coefs_m1[i,2]*fRfR$FemaleDemocrat + #Female Democrat (obs val)
                                  sim_coefs_m1[i,3]*1 + #FemaleRepublican 
                                  sim_coefs_m1[i,4]*fRfR$female2 + 
                                  sim_coefs_m1[i,5]*fRfR$GenerationZ +
                                  sim_coefs_m1[i,6]*fRfR$Millennials +
                                  sim_coefs_m1[i,7]*fRfR$GenerationX +
                                  sim_coefs_m1[i,8]*fRfR$SilentGeneration+
                                  sim_coefs_m1[i,9]*fRfR$rep +
                                  sim_coefs_m1[i,10]*fRfR$otherparty+
                                  sim_coefs_m1[i,11]*fRfR$voted2016r+
                                  sim_coefs_m1[i,12]*fRfR$Proptrumpvote2016+
                                  sim_coefs_m1[i,13]*fRfR$totalreceipts+
                                  sim_coefs_m1[i,14]*fRfR$female2*fRfR$FemaleDemocrat+ #Female Democrat*Female
                                  sim_coefs_m1[i,15]*fRfR$female2*1+ #FemaleRepublican:female2
                                  sim_coefs_m1[i,16]*fRfR$GenerationZ*fRfR$FemaleDemocrat + #FemaleDemocrat:GenerationZ
                                  sim_coefs_m1[i,17]*fRfR$GenerationZ*1 + #FemaleRepublican:GenerationZ
                                  sim_coefs_m1[i,18]*fRfR$Millennials*fRfR$FemaleDemocrat + #FemaleDemocrat:Millennials
                                  sim_coefs_m1[i,19]*fRfR$Millennials*1 + #FemaleRepublican:Millennials
                                  sim_coefs_m1[i,20]*fRfR$GenerationX*fRfR$FemaleDemocrat + #FemaleDemocrat:GenerationX
                                  sim_coefs_m1[i,21]*fRfR$GenerationX*1 + #FemaleRepublican:GenerationX
                                  sim_coefs_m1[i,22]*fRfR$SilentGeneration*fRfR$FemaleDemocrat+ #FemaleDemocrat:SilentGeneration
                                  sim_coefs_m1[i,23]*fRfR$SilentGeneration*1+ #FemaleRepublican:SilentGeneration
                                  sim_coefs_m1[i,24]*fRfR$rep*fRfR$FemaleDemocrat + #FemaleDemocrat:rep
                                  sim_coefs_m1[i,25]*fRfR$rep*1 + #FemaleRepublican:rep
                                  sim_coefs_m1[i,26]*fRfR$otherparty*fRfR$FemaleDemocrat+ #FemaleDemocrat:otherparty
                                  sim_coefs_m1[i,27]*fRfR$otherparty*1+ #FemaleRepublican:otherparty
                                  sim_coefs_m1[i,28]*fRfR$female2*fRfR$rep+ #female2:rep
                                  sim_coefs_m1[i,29]*fRfR$female2*fRfR$otherparty+ #female2:otherparty
                                  sim_coefs_m1[i,30]*fRfR$female2*fRfR$otherparty*fRfR$FemaleDemocrat+ #FemaleDemocrat:female2:otherparty
                                  sim_coefs_m1[i,31]*fRfR$female2*fRfR$otherparty*1+ #FemaleRepublican:female2:otherparty
                                  sim_coefs_m1[i,32]*fRfR$female2*fRfR$rep*fRfR$FemaleDemocrat+ #FemaleDemocrat:female2:rep
                                  sim_coefs_m1[i,33]*fRfR$female2*fRfR$rep*1 #FemaleRepublican:female2:rep
  ))
}
mean(w1.fRfRm1) #

effect.fRfRm1	<- w1.fRfRm1 - w0.fRfRm1 
quantile(effect.fRfRm1, c(.025,.975)) #to get CIs; 
mean(effect.fRfRm1) #to get mean effect

#Drop what we no longer need.
rm(fRfR, w1.fRfRm1, w0.fRfRm1 )

#Step 5.Store the Effects.----------------------
Election<-c(rep("2018 General",11))
Effect<-c(meaneffect_m1, mean(effect.FDEM), mean(effect.FGOP), mean(effect.DEM), mean(effect.fDmDEMm1), mean(effect.fDfDEMm1), mean(effect.fDmREPm1),mean(effect.fDfREPm1),mean(effect.fDmOPm1),mean(effect.fDfOPm1),mean(effect.fRfRm1))
LowerCI<-c(lowerci_m1,  quantile(effect.FDEM, c(.025)), quantile(effect.FGOP, c(.025)), quantile(effect.DEM, c(.025)),quantile(effect.fDmDEMm1, c(.025)),quantile(effect.fDfDEMm1, c(.025)),quantile(effect.fDmREPm1, c(.025)),quantile(effect.fDfREPm1, c(.025)),quantile(effect.fDmOPm1, c(.025)),quantile(effect.fDfOPm1, c(.025)),quantile(effect.fRfRm1, c(.025)))
UpperCI<-c(upperci_m1, quantile(effect.FDEM, c(.975)), quantile(effect.FGOP, c(.975)), quantile(effect.DEM, c(.975)),quantile(effect.fDmDEMm1, c(.975)),quantile(effect.fDfDEMm1, c(.975)),quantile(effect.fDmREPm1, c(.975)),quantile(effect.fDfREPm1, c(.975)),quantile(effect.fDmOPm1, c(.975)),quantile(effect.fDfOPm1, c(.975)),quantile(effect.fRfRm1, c(.975)))
PAgeneral<-data.frame(Effect, UpperCI, LowerCI, Election) 
PAgeneral$`Registrant Subgroup`<-factor(c(1,2,3,4,5,6,7,8,9,10,11), levels=c(1,2,3,4,5,6,7,8,9,10,11),
                                        labels=c("All Female\n Candidates", "Female Democratic\n Candidates", "Female Republican\n Candidates", "Female Democrats\n on all Democrats","Female Democrats\n on Democratic Men", "Female Democrats\n on Democratic Women","Female Democrats\n on Republican Men", "Female Democrats\n on Republican Women", "Female Democrats\n on Other Party Men", "Female Democrats\n on Other Party Women", "Female Republicans\n on Republican Women"), ordered = T)
#write effects to data frame.
write.csv(PAgeneral, "PA2018GeneralEffectsInteractivePartyRevised.csv", row.names=F)


